// Fill out your copyright notice in the Description page of Project Settings.


#include "UserWidget/WidgetComponent/SaveGameWidget.h"
#include "Components/Border.h"
#include "Components/TextBlock.h"
#include "Libraries/CommonLibrary.h"
#include "Materials/MaterialInstanceDynamic.h"

void USaveGameWidget::NativeOnInitialized() {
	IncreaseValue = 0.02f;
}

void USaveGameWidget::NativeDestruct() {
	GetWorld()->GetTimerManager().ClearTimer(TimerBarHandle);
}

void USaveGameWidget::ShowSaveWidget(float DisplayTime, FText InSaveText, FLinearColor TextColor, FLinearColor LargeBarColor, FLinearColor MediumBarColor, FLinearColor SmalBarColor)
{
	SavingText->SetText(InSaveText);
	SavingText->SetColorAndOpacity(FSlateColor(TextColor));
	Bar->SetBrushColor(LargeBarColor);
	Bar2->SetBrushColor(MediumBarColor);
	Bar3->SetBrushColor(SmalBarColor);
	SetVisibility(ESlateVisibility::Visible);
	PlayAnimation(UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("TextAnim")), 0.f, 1);
	Bar->GetDynamicMaterial()->SetScalarParameterValue(FName("Percent"), Bar1Progress);
	Bar2->GetDynamicMaterial()->SetScalarParameterValue(FName("Percent"), Bar2Progress);
	Bar3->GetDynamicMaterial()->SetScalarParameterValue(FName("Percent"), Bar3Progress);
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject(this, &USaveGameWidget::BarProgress);
	GetWorld()->GetTimerManager().SetTimer(TimerBarHandle, RespawnDelegate, 0.01, true);
	DelayLater(DisplayTime);
}

void USaveGameWidget::BarProgress() {
	Bar1Progress = FMath::Clamp(Bar1Progress + IncreaseValue, 0.f, 1.f);
	Bar->GetDynamicMaterial()->SetScalarParameterValue(FName("Percent"), Bar1Progress);
	if (Bar1Progress == 1.f) {
		Bar2Progress = FMath::Clamp(Bar2Progress + IncreaseValue, 0.f, 1.f);
		Bar2->GetDynamicMaterial()->SetScalarParameterValue(FName("Percent"), Bar2Progress);
		if (Bar2Progress == 1.f) {
			Bar3Progress = FMath::Clamp(Bar3Progress + IncreaseValue, 0.f, 1.f);
			Bar3->GetDynamicMaterial()->SetScalarParameterValue(FName("Percent"), Bar3Progress);
			if (Bar3Progress == 1.f) {
				GetWorld()->GetTimerManager().ClearTimer(TimerBarHandle);
				ResetBars();
			}
		}
	}
}

void USaveGameWidget::ResetBars() {
	Bar1Progress = 0.f;
	Bar->GetDynamicMaterial()->SetScalarParameterValue(FName("Percent"), Bar1Progress);
	Bar2Progress = 0.f;
	Bar2->GetDynamicMaterial()->SetScalarParameterValue(FName("Percent"), Bar2Progress);
	Bar3Progress = 0.f;
	Bar3->GetDynamicMaterial()->SetScalarParameterValue(FName("Percent"), Bar3Progress);
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject(this, &USaveGameWidget::BarProgress);
	GetWorld()->GetTimerManager().SetTimer(TimerBarHandle, RespawnDelegate, 0.01, true);
}

void USaveGameWidget::DelayLater(float DisplayTime) {
	GetWorld()->GetTimerManager().ClearTimer(DelayHandle);
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject(this, &USaveGameWidget::DelayCall);
	GetWorld()->GetTimerManager().SetTimer(DelayHandle, RespawnDelegate, DisplayTime, false);
}

void USaveGameWidget::DelayCall() {
	SetVisibility(ESlateVisibility::Collapsed);
	Bar1Progress = 0.f;
	Bar2Progress = 0.f;
	Bar3Progress = 0.f;
	GetWorld()->GetTimerManager().ClearTimer(TimerBarHandle);
}

